package com.ceba.test.modules.email.dao;

import abc.data.DataRow;
import com.ceba.cebautils.DateUtil;
import com.ceba.test.base.configs.CebaConfig;
import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.modules.email.entity.MsgMail;
import com.ceba.test.modules.email.entity.MsgMailSend;
import com.ceba.test.modules.email.entity.UserInfoSet;

import abc.ID;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;
import abc.user.IUser;

import java.util.ArrayList;
import java.util.List;

public class MsgMailDao extends IBaseDao {
	
	
	public boolean deleteMailInfo(String name,String UserId) throws Exception{
		Sql sql = new Sql("delete from msg_mail where ID = '"+UserId+"' AND USER_ID='"+UserId+"' AND NAME='"+name+"'");
		return SqlSessionManager.execute(sql);
	}
	
	
   public Boolean saveUserMailInfo(MsgMail mail) throws Exception{
	   IStore store = StoreManager.create("msg_mail");
	   IObject object = new XObject("msg_mail");
	    object.put("ID",mail.getId());
		object.put("USER_ID",mail.getUserId());
		object.put("MAIL",mail.getMail());
		object.put("NAME",mail.getName());
		object.put("SMTP",mail.getSmtp());
		object.put("PASSWORD",mail.getPassword());
		if(store.save(object)>0){
			return true;
		}
		return false;
   }

	
	public MsgMail getUserMailInfo(String loginUserId) throws Exception{		
		IStore store = StoreManager.create("msg_mail");
		IObject object = null;
		if(null != loginUserId){
			object = store.get(loginUserId);
		}
		if(object==null){
			 object = store.get("1");
		}
		else{
			if(object.getString("MAIL").equals("") || 
					object.getString("SMTP").equals("") || object.getString("PASSWORD").equals("")){
				 object = store.get("1");
			}
		}
		MsgMail mail = new MsgMail();
		getMsgMailFromObject(mail,object);
		return mail;
	}
	
	public void getMsgMailFromObject(MsgMail mail,IObject object) throws Exception{
		mail.setSmtp(object.getString("SMTP")); // 邮件服务器地址
		mail.setMail(object.getString("MAIL")); // 用户名
		mail.setPassword(CebaConfig.getDecode(object.getString("PASSWORD"))); // 密码
		String name = object.getString("NAME");
		mail.setUserId(object.getString("USER_ID"));
		mail.setId(object.getString("ID"));
		mail.setName(name);
		mail.setContent(object.getString("CONTENT"));
	}
	
	/**
	 * 添加一个常用的EMAIL
	 * @param mail email
	 * @return
	 */
	public Boolean addSendEMail(String mail,String loginUserId)throws Exception{
		if(!SqlSessionManager.existRecord("msg_mail_send", "EMAIL='"+mail+"'")){
			
			MsgMailSend msgMailSend = new MsgMailSend();
			msgMailSend.setId(ID.getGuid());
			msgMailSend.setEmail(mail);
			msgMailSend.setCreateTime(DateUtil.getNowTime(""));
			msgMailSend.setCreateUser(loginUserId);
			return addSendEMail(msgMailSend);
		}
				
		return false;

	}
	
	/**
	 * 添加一个常用的EMAIL
	 * @return
	 */
	public Boolean addSendEMail(MsgMailSend mailSend)throws Exception{
		IStore store = StoreManager.create("msg_mail_send");
		XObject object = new XObject("msg_mail_send");
		object.put("ID", mailSend.getId());
		object.put("EMAIL", mailSend.getEmail());
		object.put("CREATE_TIME",mailSend.getCreateTime());
		object.put("CREATE_USER",mailSend.getCreateUser());
		if (store.insert(object) > 0) {
			return true;
		}	
		return false;
	}
   
   
	/**
	 * 保存邮件配置
	 * @param mail
	 * @return
	 */
	public boolean addMsgEmail(MsgMail mail) {
		IStore store = StoreManager.create("msg_mail");
		XObject object = new XObject("ID","1");
		object.put("MAIL", mail.getMail());
		object.put("NAME", mail.getName());
		object.put("SMTP", mail.getSmtp());
		object.put("PASSWORD", mail.getPassword());
		if(store.save(object)<0){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 获取msg_mail的单个信息
	 * 
	 */
	public MsgMail getMsgMailById(String id) throws Exception{
		MsgMail msgMail = new MsgMail();
		IStore store = StoreManager.create("msg_mail");
		IObject object = store.get(id);
		msgMail.setMail(object.getString("MAIL"));
		msgMail.setSmtp(object.getString("SMTP"));
		msgMail.setName(object.getString("NAME"));
		return msgMail;
	}
	
	/**
	 * 根据id获取
	 * @return
	 * @throws Exception
	 */
	public  DataTable getMsgMailByUserId(String id) throws Exception{
		Sql sql  = new Sql("SELECT * FROM msg_mail where ID='"+id+"'");
		return SqlSessionManager.getDataTable(sql);
	}
	
	public UserInfoSet getUserInfoSet(IUser user){
		UserInfoSet userInfo = new UserInfoSet();
 		IStore store = StoreManager.create("msg_mail");
		IObject object = store.get(user.getId());
		String mail="";
		String smtp="";
		String content="";
		String name=user.getName();
		userInfo.setRole((String) user.toDictionary().get("S_ROLE"));
 		if(object!=null){
 			userInfo.setMail(object.getString("MAIL"));
 			userInfo.setSmtp(object.getString("SMTP"));
 			userInfo.setName(object.getString("NAME"));
 			userInfo.setContent(object.getString("CONTENT"));
 		}
	 return userInfo;
 }
	
	/**
	 * 管理员修改个性签名
	 * @param content
	 * @return
	 */
	public boolean getMsgMailAddContent(String content){
		Sql sql = new Sql("update msg_mail set content='"+content+"' where id='1'");
		return SqlSessionManager.execute(sql);
	}

	public List<MsgMail> getAllMsgMail() {
		Sql sql = new Sql("select ID,CONTENT from msg_mail ");
		DataTable dataTable=SqlSessionManager.getDataTable(sql);
		if(dataTable != null && dataTable.getRowCount() > 0){
			List<MsgMail> msgMailList=new ArrayList<>();
			for(DataRow row:dataTable.getRows()){
				MsgMail msgMail=new MsgMail();
				msgMail.setId(row.getString("ID"));
				msgMail.setContent(row.getString("CONTENT"));
				msgMailList.add(msgMail);
			}
			return msgMailList;
		}
		return null;
	}

	public boolean updateContent(String id, String value) {
		IStore store = StoreManager.create("msg_mail");
		XObject object = new XObject("ID", id);
		object.put("CONTENT", value);
		if (store.update(object) < 0) {
			return true;
		}
		return false;
	}
	
	
}
